package com.me.bbs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.anno.RequireLogin;
import com.me.bbs.common.Resp;
import com.me.bbs.common.SessionUtils;
import com.me.bbs.common.TimeUtils;
import com.me.bbs.entity.Comment;
import com.me.bbs.service.ICommentService;
import com.me.bbs.service.ILatestReadAtService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lm
 * @since 2023-01-24
 */
@RestController
@RequestMapping("/unread")
public class LatestReadAtController {
    @Resource
    private ICommentService commentService;
    @Resource
    private ILatestReadAtService latestReadAtService;
    @PostMapping("/reply")
    @RequireLogin
    public Resp<?> getLastestReplies(@RequestParam("current") Long current,
                                     @RequestParam("size") Long size,
                                     @RequestParam("t") Long t) {
        Page<Comment> page = new Page<>(current, size);
        Long uid = (Long) SessionUtils.getAttribute("user");
        LocalDateTime time  = TimeUtils.timestampToTime(t);
        Map<String ,Object> argsMap = new HashMap<>();
        argsMap.put("uid", uid);
        argsMap.put("t",time);
        Page<Map<String, Object>> latestComments = commentService.getLatestComments(page, argsMap);
        return  Resp.success(latestComments,"成功");
    }

    @PostMapping("/reply/count")
    @RequireLogin
    public Resp<?> getUnreadReplyCount(@RequestParam("t") Long time) {
        Long uid = (Long) SessionUtils.getAttribute("user");
        LocalDateTime t = TimeUtils.timestampToTime(time);
        Map<String ,Object> argsMap = new HashMap<>();
        argsMap.put("uid", uid);
        argsMap.put("t",t);
        Long unreadCount = commentService.getUnreadCount(argsMap);
        return Resp.success(unreadCount,"成功");
    }

    @PostMapping("/zan/count")
    @RequireLogin
    public Resp<?> getUnreadZanCount(@RequestParam("t") Long time) {
        Long uid = (Long) SessionUtils.getAttribute("user");
        LocalDateTime t = TimeUtils.timestampToTime(time);
        Map<String ,Object> argsMap = new HashMap<>();
        argsMap.put("uid", uid);
        argsMap.put("t",t);
        Long unreadCount = latestReadAtService.getUnreadZanCount(argsMap);
        return Resp.success(unreadCount,"成功");
    }

    @PostMapping("/zan")
    @RequireLogin
    public Resp<?> getLatestZan(@RequestParam("current") Long current,
                                @RequestParam("size") Long size,
                                @RequestParam("t") Long t) {
        Page page = new Page<>(current, size);
        Long uid = (Long) SessionUtils.getAttribute("user");
        LocalDateTime time  = TimeUtils.timestampToTime(t);
        Map<String ,Object> argsMap = new HashMap<>();
        argsMap.put("uid", uid);
        argsMap.put("t",time);
        Page<Map<String, Object>> unreadZanMsg = latestReadAtService.getUnreadZanMsg(page, argsMap);
        return  Resp.success(unreadZanMsg,"成功");
    }
}
